from flask import Flask
from flask_apscheduler import APScheduler

from config.config import cache_config, Config
from services.my_service import MyService
from utils.cache_helper import CacheHelper
from utils.log_handler import LogHandler
from config.config import cache_config, Config  # 导入Config类的配置
from utils import init_scheduler  # 导入init_scheduler方法
# 创建Flask应用
app = Flask(__name__)
app.config.from_object(Config)  # 读取Config类的配置
init_scheduler(app)  # init_scheduler方法

# 添加日志配置
for log_handler in LogHandler.get_log_handlers():
    app.logger.addHandler(log_handler)

# # 初始化APScheduler
# scheduler = APScheduler()
# 方式一硬编码
# scheduler.add_job(func=MyService.my_job, id='my_job', trigger='cron', second='0/5')
# # 方式二读取配置
# app.config.from_object(Config())
# scheduler.init_app(app=app)
# scheduler.start()
# app.config.from_object(Config())
# init_scheduler(app)

# 初始化cache,硬编码方式配置缓存
# cache = CacheHelper(app, config={'CACHE_TYPE': 'simple'})
# 初始化cache,读取配置文件方式配置缓存
cache = CacheHelper(app, config=cache_config)


@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'


# 写入缓存
@app.route('/api/SetCache')
def set_cache():
    MyService.set_my_cache()
    return 'success'


# 读取缓存
@app.route('/api/GetCache')
def get_cache():
    my_cache = MyService.get_my_cache()
    return my_cache


if __name__ == '__main__':

    app.run()
